package com.sdry.service.impl.hpf;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.CacheAreaMapper;
import com.sdry.mapper.hpf.PartMapper;
import com.sdry.mapper.hpf.PickApplyDetailMapper;
import com.sdry.mapper.hpf.PickApplyMapper;
import com.sdry.model.hpf.CacheArea;
import com.sdry.model.hpf.Part;
import com.sdry.model.hpf.PickApply;
import com.sdry.model.hpf.PickApplyDetail;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.User;
import com.sdry.service.hpf.CacheAreaService;
import com.sdry.utils.StringUtil;

/**
 * @ClassName: CacheAreaServiceimpl 
 * @Description:缓存区Service实现
 * @Author hpf
 * @Date 2018年11月13日 下午5:18:05 
 * @version 1.0
 */
@Service("cacheAreaService")
public class CacheAreaServiceimpl implements CacheAreaService {

	@Resource
	private CacheAreaMapper cacheAreaMapper;
	@Resource
	private PartMapper partMapper;
	@Resource
	private PickApplyMapper pickApplyMapper;
	@Resource
	private PickApplyDetailMapper pickApplyDetailMapper;

	/**
	 * 缓存区总数
	 * @param cacheArea
	 * @return
	 */
	public Integer countCacheArea(CacheArea cacheArea) {
		// TODO Auto-generated method stub
		return cacheAreaMapper.countCacheArea(cacheArea);
	}

	/**
	 * 分页查询缓存区
	 * @param map
	 * @return
	 */
	public List<CacheArea> listPageCacheArea(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return cacheAreaMapper.listPageCacheArea(map);
	}

	/**
	 * 查询所有缓存区
	 * @return
	 */
	public List<CacheArea> listAllCacheArea() {
		// TODO Auto-generated method stub
		return cacheAreaMapper.listAllCacheArea();
	}

	/**
	 * 叫料批量新增缓存区
	 * @param paID 配件ID
	 * @param batch 批次号
	 * @param quantity 数量
	 * @param session
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer insertCacheArea(Long[] paID,String[] batch,String[] quantity,HttpSession session) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			PickApply pickApply = new PickApply();
			User user = (User) session.getAttribute("currentUser");
			Department department = (Department) session.getAttribute("department");
			pickApply.setPickDept(department.getDid());
			pickApply.setPickPerson(user.getUid());
			pickApply.setReserve1("0");
			pickApply.setPickNum("P"+getRandomNumber(12));
			pickApply.setPickDate(new Date());
			pickApply.setDemandDate(new Date());
			pickApply.setReserve5("1");
			int pickID = pickApplyMapper.insertPickApply(pickApply);
			isSuccess = pickID;
			for (int i = 0; i < paID.length; i++) {
				Part part = partMapper.findPartByPaid(paID[i]);
				PickApplyDetail pickApplyDetail = new PickApplyDetail();
				pickApplyDetail.setPickID(Long.parseLong(pickID+""));
				pickApplyDetail.setStockNum(part.getPanum());
				pickApplyDetail.setStockName(part.getPaname());
				pickApplyDetail.setStockModel(part.getPamodel());
				pickApplyDetail.setReserve1(part.getParemark1());
				pickApplyDetail.setReserve2(part.getParemark2());
				pickApplyDetail.setReserve3(batch[i]);
				pickApplyDetail.setQuantity(StringUtil.calcFloatValue(quantity[i], "0", "add"));
				isSuccess = pickApplyDetailMapper.insertPickApplyDetail(pickApplyDetail);
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}
	
	/**
	 * 新增缓存区
	 * @param paID 配件ID
	 * @param batch 批次号
	 * @param quantity 数量
	 * @return
	 */
	public Integer insert(Long paID,String batch,String quantity) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		CacheArea cacheArea = cacheAreaMapper.findCacheAreaByPaID(paID);
		if (cacheArea != null) {
			if (cacheArea.getBatch().equals(batch)) {
				String addQuantity = StringUtil.calcFloatValue(cacheArea.getQuantity(), quantity, "and");
				cacheArea.setQuantity(addQuantity);
				isSuccess = cacheAreaMapper.updateCacheArea(cacheArea);
			} else {
				CacheArea cacheArea2 = new CacheArea();
				cacheArea2.setPaID(paID);
				cacheArea2.setBatch(batch);
				cacheArea2.setQuantity(quantity+0F);
				isSuccess = cacheAreaMapper.insertCacheArea(cacheArea2);
			}
		} else {
			CacheArea cacheArea2 = new CacheArea();
			cacheArea2.setPaID(paID);
			cacheArea2.setBatch(batch);
			cacheArea2.setQuantity(quantity+0F);
			isSuccess = cacheAreaMapper.insertCacheArea(cacheArea2);
		}
		return isSuccess;
	}

	/**
	 * 跟新缓存区
	 * @param cacheArea 缓存区实体
	 * @return
	 */
	public Integer updateCacheArea(CacheArea cacheArea) {
		// TODO Auto-generated method stub
		return cacheAreaMapper.updateCacheArea(cacheArea);
	}

	/**
	 * 根据配件ID查询缓存区
	 * @param paID 配件ID
	 * @return
	 */
	public CacheArea findCacheAreaByPaID(Long paID) {
		// TODO Auto-generated method stub
		return cacheAreaMapper.findCacheAreaByPaID(paID);
	}

	/**
	 * 缓存区盘点
	 * @param cacheAreaList
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer cacheAreaCheck(List<CacheArea> cacheAreaList) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			for (int i = 0; i < cacheAreaList.size(); i++) {
				isSuccess = cacheAreaMapper.cacheAreaCheck(cacheAreaList.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}
	
	/**
	 * 生成随机数
	 * @param digCount 随机数位数
	 * @return
	 */
	public static String getRandomNumber(int digCount) {
		Random rnd = new Random();
	    StringBuilder sb = new StringBuilder(digCount);
	    for(int i=0; i < digCount; i++)
	        sb.append((char)('0' + rnd.nextInt(10)));
	    return sb.toString();
	}

	@Transactional(rollbackFor={Exception.class})
	public Integer cacheAreaEmpty(List<CacheArea> cacheAreaList) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			for (int i = 0; i < cacheAreaList.size(); i++) {
				isSuccess = cacheAreaMapper.cacheAreaEmpty(cacheAreaList.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

	/**
	 * 设置预警值
	 * @param cacheArea 缓存区实体
	 * @return
	 */
	public Integer setWarningValue(CacheArea cacheArea) {
		// TODO Auto-generated method stub
		return cacheAreaMapper.setWarningValue(cacheArea);
	}
}
